package com.shouer.doc.ui.conversation;

import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.shouer.doc.R;
import com.shouer.doc.common.ShouerConst;
import com.shouer.doc.common.util.AppUtil;
import com.shouer.doc.common.util.DateFormatUtil;
import com.shouer.doc.common.util.ScreenUtils;
import com.shouer.doc.ui.conversation.adapter.PatRecordImageAdapter;
import com.shouer.doc.ui.neworder.model.SimpleOrderModel;
import com.shouer.doc.widget.HorItemDecoration;
import com.shouer.doc.widget.ShouerLayoutManager;
import com.shouer.doc.widget.behavior.SheetBehavior;

import java.text.ParseException;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ZXL on 2016/12/7.
 */

public class ConversationPatientView extends LinearLayout implements View.OnClickListener{

    @BindView(R.id.order_detail)
    protected View patientDetailView;
    @BindView(R.id.pat_ico)
    protected SimpleDraweeView patIco;
    @BindView(R.id.pat_name)
    protected TextView patNameView;
    @BindView(R.id.gender)
    protected TextView patGenderView;
    @BindView(R.id.age)
    protected TextView patAgeView;
    @BindView(R.id.create_time)
    protected TextView createTimeView;
    @BindView(R.id.close_btn)
    protected View closeBtn;
    @BindView(R.id.description_layout)
    protected View descriptionLayout;
    @BindView(R.id.description_txt)
    protected TextView descriptionView;
    @BindView(R.id.pat_img_layout)
    protected View patImgLayout;
    @BindView(R.id.img_list)
    protected RecyclerView imgList;
    @BindView(R.id.arrow)
    protected ImageView arrowImg;
    @BindView(R.id.arrow_layout)
    protected View arrowLayout;
    @BindView(R.id.loading)
    protected View isLoaingView;
    @BindView(R.id.loading_failed)
    protected View loadFailView;
    @BindView(R.id.vip_ico)
    protected View vipIco;

    @BindView(R.id.kouka)
    protected View koukaView;
    @BindView(R.id.user_balance)
    protected TextView userBalanceView;

    private SheetBehavior behavior;

    public ConversationPatientView(Context context) {
        super(context);
        init(context);
    }

    public ConversationPatientView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ConversationPatientView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public ConversationPatientView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.layout_conversation_patient_layout, this);
        ButterKnife.bind(this);
    }

    @Override
    public void onAttachedToWindow()
    {
        super.onAttachedToWindow();
        arrowLayout.setOnClickListener(this);
        behavior = SheetBehavior.from(this);
        if (null != behavior) {
            behavior.setBottomSheetCallback(new SheetBehavior.SheetCallback() {
                @Override
                public void onStateChanged(@NonNull View bottomSheet, @SheetBehavior.State int newState) {
                    if (newState == SheetBehavior.STATE_EXPANDED)//展开
                    {
                        arrowImg.setImageResource(R.drawable.ico_arrow_up);
                    } else if (newState == SheetBehavior.STATE_COLLAPSED)//收起
                    {
                        arrowImg.setImageResource(R.drawable.ico_arrow_down);
                    }
                }

                @Override
                public void onSlide(@NonNull View bottomSheet, float slideOffset) {

                }
            });
        }
    }

    public void fillPatientInfo(SimpleOrderModel orderModel)
    {
        if (orderModel==null)
        {
            loadFailView.setVisibility(GONE);
            patientDetailView.setVisibility(GONE);
            isLoaingView.setVisibility(VISIBLE);

        }else
        {
            if (orderModel.getOrderType()==ShouerConst.ORDER_TYPE_YAO)
            {
                String icoUriString="res://"+ AppUtil.getPackageName(getContext())+"/"+R.drawable.ico_order_type_yao;
                Uri uri=Uri.parse(icoUriString);
                patIco.setImageURI(uri);
                patNameView.setText(orderModel.getPatient().getPatName());
                patGenderView.setVisibility(GONE);
                patAgeView.setVisibility(GONE);
                try {
                    Date createDate=DateFormatUtil.stringToDate(orderModel.getCreateTime().replace("T","\b"),"yyyy-MM-dd\bHH:mm:ss");
                    createTimeView.setText(DateFormatUtil.dateToString(createDate,"yyyy-MM-dd HH:mm"));
                } catch (Exception e) {
                    Log.e(ConversationPatientView.class.getSimpleName(),e.toString());
                    createTimeView.setText("-");
                }

                descriptionLayout.setVisibility(GONE);
                patImgLayout.setVisibility(GONE);

            }else
            {
                String icoUriString="res://"+ AppUtil.getPackageName(getContext())+"/"+R.drawable.ico_patient_default_touxiang;
                Uri uri=Uri.parse(icoUriString);
                patIco.setImageURI(uri);
                patNameView.setText(orderModel.getPatient().getPatName());
                patGenderView.setText(orderModel.getPatient().getPatSex()==0?"女":"男");
                int age = 0;
                try {
                    age = DateFormatUtil.getAgeByBirthday(DateFormatUtil.stringToDate(orderModel.getPatient().getPatBirthday().replace("T", "\b"), "yyyy-MM-dd"));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                age = age < 0 ? 0 : age;
                patAgeView.setText(getResources().getString(R.string.patient_age, age));
                try {
                    Date createDate=DateFormatUtil.stringToDate(orderModel.getCreateTime().replace("T","\b"),"yyyy-MM-dd\bHH:mm:ss");
                    createTimeView.setText(DateFormatUtil.dateToString(createDate,"yyyy-MM-dd HH:mm"));
                } catch (Exception e) {
                    Log.e(ConversationPatientView.class.getSimpleName(),e.toString());
                    createTimeView.setText("-");
                }
                if (null==orderModel.getPatRecord()|| TextUtils.isEmpty(orderModel.getPatRecord().getPatDescribe()))
                {
                    descriptionView.setText("无");
                }else
                {
                    descriptionView.setText(orderModel.getPatRecord().getPatDescribe());
                }
                ShouerLayoutManager imgLayoutManager=new ShouerLayoutManager(getContext());
                imgLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                imgList.setLayoutManager(imgLayoutManager);
                imgList.addItemDecoration(new HorItemDecoration(ScreenUtils.dip2px(getContext(),12),false));
                if (null==orderModel.getPatRecord()||orderModel.getPatRecord().getPatPages().size()==0)
                {
                    patImgLayout.setVisibility(View.GONE);
                }else
                {
                    imgList.setAdapter(new PatRecordImageAdapter(getContext(),orderModel.getPatRecord().getPatPages()));
                    patImgLayout.setVisibility(View.VISIBLE);
                }

                loadFailView.setVisibility(GONE);
                isLoaingView.setVisibility(GONE);
                patientDetailView.setVisibility(VISIBLE);
            }


            if (orderModel.getOrderState()== ShouerConst.ORDER_STATE_CLOSED||orderModel.getOrderState()== ShouerConst.ORDER_STATE_EVALUATED)
            {
                closeBtn.setVisibility(INVISIBLE);
            }

        }
    }

    public void loadDetailFailed()
    {
        patientDetailView.setVisibility(GONE);
        isLoaingView.setVisibility(GONE);
        loadFailView.setVisibility(VISIBLE);
    }

    @Override
    public void onClick(View view) {

        if (behavior!=null)
        {
            if (behavior.getState() == SheetBehavior.STATE_EXPANDED)//展开
            {
                behavior.collapsed();
            } else if (behavior.getState() == SheetBehavior.STATE_COLLAPSED)//收起
            {
                behavior.expand();
            }
        }
    }

    public void showVIP()
    {
        vipIco.setVisibility(VISIBLE);
    }

    public void showKouka()
    {
        koukaView.setVisibility(VISIBLE);
    }

    public void setUserBalance(String userBalance)
    {
        userBalance=TextUtils.isEmpty(userBalance)?"0":userBalance;
        userBalanceView.setText("￥"+userBalance);
    }
}
